package com.hd.manager.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hd.appcommon.entity.OpenRecord;
import com.hd.appcommon.entity.vo.OpenRecordVo;
import com.hd.appcommon.service.OpenRecordService;
import com.hd.hdxgddcommon.controller.AbstractController;
import com.hd.hdxgddcommon.entity.dto.ResultDto;
import com.hd.hdxgddcommon.page.MyPage;
import com.hd.manager.util.JieShiActivateRequestCallBackUtil;
import com.hd.manager.util.JieShiActivateRequestUtil;
import com.hd.manager.webSocket.DeviceEndpoint;
import com.taobao.api.internal.dto.RequestDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.OnMessage;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Locale;

import static com.hd.manager.webSocket.EndpointManager.getEnpoint;

/**
 * @projectName: entrance_guard
 * @package: com.hd.manager.controller
 * @className: JieShiUserController
 * @author: 习
 * @description: TODO
 * @date: 2023/7/23 16:16
 * @version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/JieShi")
public class JieShiUserController extends AbstractController {
    @Autowired
    private OpenRecordService openRecordService;
    @Autowired
    private JieShiActivateRequestUtil jieShiActivateRequestUtil;
    @Autowired
    private JieShiActivateRequestCallBackUtil jieShiActivateRequestCallBackUtil;

    /**
     *
     * @param requestId 请求Id序列码
     * @param image 图像base64字符串
     * @param name 用户姓名
     * @param phoneNumber 用户电话
     * @Return 是否执行成功
     * @return
     */
    @RequestMapping("/AddUser")
    @OnMessage
    public ResultDto AddUser(String requestId, String deviceId, String imageType, @RequestBody String image, String userId, String name, String phoneNumber, String pass) throws InterruptedException {
        HashMap<String,String> userInfo = new HashMap();
        userInfo.put("name",name);
        userInfo.put("phone_number",phoneNumber);
        HashMap<String,Object> requestMap = new HashMap<>();
        requestMap.put("user_info",userInfo);
        requestMap.put("request_type","addUser");
        requestMap.put("device_id",deviceId);
        requestMap.put("request_id",requestId);
        requestMap.put("image_type",imageType);
        requestMap.put("image_content",image.trim());
        requestMap.put("user_id",userId);
        requestMap.put("pass",pass);
        JSONObject res = jieShiActivateRequestUtil.activateRequest(requestMap);
        return res!=null?success(res):fail(String.valueOf(res));
    }

    /**
     *
     * @param requestId 请求Id序列码
     * @param image 图像base64字符串
     * @param name 用户姓名
     * @param phoneNumber 用户电话
     * @Return 是否执行成功
     * @return
     */
    @RequestMapping("/UpdateUser")
    @OnMessage
    public ResultDto UpdateUser(String requestId, String deviceId, String imageType, @RequestBody String image, String userId, String name, String phoneNumber, String pass) throws InterruptedException {
        HashMap<String,String> userInfo = new HashMap();
        userInfo.put("name",name);
        userInfo.put("phone_number",phoneNumber);
        HashMap<String,Object> requestMap = new HashMap<>();
        requestMap.put("user_info",userInfo);
        requestMap.put("request_type","updateUser");
        requestMap.put("device_id",deviceId);
        requestMap.put("request_id",requestId);
        requestMap.put("image_type",imageType);
        requestMap.put("image_content",image.trim());
        requestMap.put("user_id",userId);
        requestMap.put("pass",pass);
        JSONObject res = jieShiActivateRequestUtil.activateRequest(requestMap);
        return res!=null?success(res):fail(String.valueOf(res));
    }




    /**
     * 请求用户userId列表
     * @param requestId
     * @param pass
     * 等待设备主动发回信息
     */
    @RequestMapping("getUserList")
    public ResultDto getUserList(String requestId,String deviceId, String pass) throws InterruptedException {
        HashMap<String,Object> requestMap = new HashMap<>();
        requestMap.put("device_id",deviceId);
        requestMap.put("request_type","getUserList");
        requestMap.put("request_id",requestId);
        requestMap.put("pass",pass);
        JSONObject res = jieShiActivateRequestUtil.activateRequest(requestMap);
        return res!=null?success(res):fail(String.valueOf(res));
    }

    /**
     *
     * @param requestId
     * @param pass
     * @return
     */
    // 获取用户userId列表后回调依此获取用户信息
    @RequestMapping("getUserInfo")
    public ResultDto getUser(String requestId,String deviceId, String pass,String userId) throws InterruptedException {
        HashMap<String,Object> requestMap = new HashMap<>();
        requestMap.put("request_type","getUserInfo");
        requestMap.put("device_id",deviceId);
        requestMap.put("request_id",requestId);
        requestMap.put("user_id",userId);
        requestMap.put("pass",pass);
        JSONObject res = jieShiActivateRequestUtil.activateRequest(requestMap);
        return res!=null?success(res):fail(String.valueOf(res));
    }

    /**
     *
     * @param requestId
     * @param deviceId
     * @param userId
     * @param pass
     */
    @RequestMapping("deleteUser")
    public ResultDto deleteUser(String requestId, String deviceId, String userId,String pass) throws InterruptedException {
        HashMap<String,Object> requestMap = new HashMap<>();
        requestMap.put("request_type","deleteUser");
        requestMap.put("request_id",requestId);
        requestMap.put("user_id",userId);
        requestMap.put("device_id",deviceId);
        requestMap.put("pass",pass);
        JSONObject res = jieShiActivateRequestUtil.activateRequest(requestMap);
        return res!=null?success(res):fail(String.valueOf(res));
    }
}
